// pages/my/replaceProduct/oldGoodsList.js
var app = getApp()
Page({
  data: {
    goodsList:[],
    goodBox:[]
  },
  onLoad: function (options) {
    app.getMemInfo().then(res=>{
      this.setData({
        agentId:res.data.agentId
      })
      this.getOldGoodsList(res.data.agentId)
    })
  },
  onShow: function () {

  },
  getOldGoodsList(agentId){
    let that = this
    let data = {
      agentId
    }
    console.log(data)
    app.request('wx_app/mall/oldForNew/oldSkuListHistory',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        let goodsList = []
        res.data.pageContent.forEach(item=>{
          console.log(item)
          item.isChecked = 0
          goodsList.push(item)
        })
        that.setData({
          goodsList
        })
      }else{
        app.errorMsg(res.message)
      }
    })
  },
  chooseItem(e){
    let i = e.currentTarget.dataset.i
    if (this.data.goodsList[i].isChecked==0) {
      let list = this.data.goodsList
      list.forEach(item=>{
        item.isChecked=0
      })
      this.setData({
        goodsList:list,
        ['goodsList['+i+'].isChecked']:1
      })

    }else{
      this.setData({
        ['goodsList['+i+'].isChecked']:0
      })
    }
    let goodsList = this.data.goodsList
    let goodBox = []
    goodsList.forEach(item=>{
      if (item.isChecked==1) {
        goodBox.push(item)
      }
      this.setData({
        goodBox
      })
    })
  },
  next(){
    if (this.data.goodBox.length==0) {
      app.errorMsg('请选择一个商品')
    }else{
      let goodInfo = JSON.stringify(this.data.goodBox[0])
      console.log(goodInfo)
      wx.navigateTo({
        url:'addExistOldGood?good='+goodInfo
      })
    }
    
  }
})